package com.projectx.action.contact;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.projectx.R;
import com.projectx.action.talk.TalkListActivity;
import com.projectx.entity.ContactsData;
import com.projectx.entity.GroupData;
import com.projectx.util.ActivityManage;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

public class SearchFriend extends AppCompatActivity {
    private Button search;
    private EditText editText;
    private ImageView delete, back;
    private RecyclerView recyclerView_person, getRecyclerView_group;
    private List<ContactsData> newContactList;
    private List<GroupData> newContactgroupList;
    private List<String> data_person = new ArrayList<>();
    private List<Bitmap> bitmaps_person;
    private List<String> data_group = new ArrayList<>();
    private List<Bitmap> bitmaps_group;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityManage.addActivity("Searchfriend", this);
        setContentView(R.layout.activity_searchfriend);
        LitePal.initialize(this);
        search = findViewById(R.id.search);
        delete = findViewById(R.id.delete);
        editText = findViewById(R.id.edittext);
        recyclerView_person = findViewById(R.id.recycler_view_person);
        getRecyclerView_group = findViewById(R.id.recycler_view_group);
        back = findViewById(R.id.back);
        for (int i = 1; i <= 5; i++) {
            data_group.add("测试1");

            data_person.add("测试2");
        }


        try {
            getSupportActionBar().hide(); //去除标题栏
        } catch (NullPointerException e) {
            Log.d("KMH", "没有状态栏标题");
        }
        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                editText.setText("");
            }
        });
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActivityManage.exitActivity("Searchfriend");
            }
        });
        editText.addTextChangedListener(new TextWatcher() {

            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }//文本改变之前执行

            @Override
            //文本改变的时候执行
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //如果长度为0
                if (s.length() == 0) {
                    //隐藏“删除”图片
                    delete.setVisibility(View.GONE);
                } else {//长度不为0
                    //显示“删除图片”
                    delete.setVisibility(View.VISIBLE);
                }
            }

            public void afterTextChanged(Editable s) {
            }//文本改变之后执行
        });

        search.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                //如果输入框内容为空，提示请输入搜索内容
                if (TextUtils.isEmpty(editText.getText().toString().trim())) {
                    Toast.makeText(SearchFriend.this, "请输入搜索内容", Toast.LENGTH_SHORT).show();
                } else {
                    newContactList = LitePal.where("username = ? and friendName like ?", TalkListActivity.username, "%" + editText.getText().toString() + "%").find(ContactsData.class);
                    newContactgroupList = LitePal.where("username = ? and groupName like ?", TalkListActivity.username, "%" + editText.getText().toString() + "%").find(GroupData.class);
                    if (newContactgroupList.isEmpty() && newContactList.isEmpty()) {
                        Toast.makeText(SearchFriend.this, "对不起，没有你要搜索的内容", Toast.LENGTH_SHORT).show();
                        // Log.d("lzw","为啥又没更新数据");
                    } else {
                        int j = newContactgroupList.size();
                        for (int i = 1; i <= j; i++) {
                            data_person.add(newContactList.get(i).getFriendName());
                            data_group.add(newContactgroupList.get(i).getGroupName());
                            bitmaps_group.add(null);
                        }

                        final RVAdapter rvAdapter;
                        rvAdapter = new RVAdapter(v.getContext(), data_person, bitmaps_person, false);
                        rvAdapter.notifyDataSetChanged();
                        final RVAdapter rvAdapter1;
                        rvAdapter1 = new RVAdapter(v.getContext(), data_group, bitmaps_group, true);
                        rvAdapter1.notifyDataSetChanged();
                        Log.d("lzw", "为啥又没更新数据");
                    }
                }
            }
        });
        recyclerView_person.setLayoutManager(new LinearLayoutManager(this));
        recyclerView_person.setAdapter(new RVAdapter(this, data_person, bitmaps_person, false));
        recyclerView_person.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        getRecyclerView_group.setLayoutManager(new LinearLayoutManager(this));
        getRecyclerView_group.setAdapter(new RVAdapter(this, data_group, bitmaps_group, true));
        getRecyclerView_group.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
    }
}
